package com.buyingtickets.service.impl;

import com.buyingtickets.dao.UnitIntervalDao;
import com.buyingtickets.entity.QueryParameter;
import com.buyingtickets.entity.Site;
import com.buyingtickets.entity.UnitInterval;
import com.buyingtickets.service.UnitIntervalBiz;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author czl
 * @version 1.0
 * @description: TODO
 * @date 2021/6/29 15:34
 */
@Service
public class UnitIntervalBizImpl implements UnitIntervalBiz {
    @Resource
    private UnitIntervalDao unitIntervalDao;

    @Override
    public List<UnitInterval> queryByQueryUnitInterval(QueryParameter qp) {
        int size = qp.getRows();//每页显示几条
        int count = queryCountQueryUnitInterval(qp);//带条件的总条数
        int countPage = count % size == 0 ? (count / size) : (count / size + 1);//带条件总页数
        if (countPage == 0) {
            return null;
        }
        int page = qp.getPage();//当前页
        //上下容错
        page = page < 1 ? 1 : page;
        page = page > countPage ? countPage : page;
        qp.setPage(page);//当前页
        qp.setStart((page - 1) * size);//偏移量
        qp.setPageCount(countPage);//总页数
        qp.setShangPage(page - 1);
        qp.setXiaPage(page + 1);
        qp.setCount(count);
        return unitIntervalDao.queryByQueryUnitInterval(qp);
    }

    @Override
    public int queryCountQueryUnitInterval(QueryParameter qp) {
        return unitIntervalDao.queryCountQueryUnitInterval(qp);
    }

    @Override
    public UnitInterval queryByID(int id) {
        return unitIntervalDao.queryByID(id);
    }

    @Override
    public int update(UnitInterval unitInterval) {
        return unitIntervalDao.update(unitInterval);
    }

    @Override
    public int delete(int id) {
        return unitIntervalDao.delete(id);
    }

    @Override
    @Transactional
    public int add(UnitInterval unitInterval) {
        boolean flag = true;
        if (unitInterval != null) {
            UnitInterval uStart = new UnitInterval();
            uStart.setStartsite(unitInterval.getStartsite());
            uStart.setEndsite(unitInterval.getStartsite());
            uStart.setMileage(0);
            UnitInterval uEnd = new UnitInterval();
            uEnd.setStartsite(unitInterval.getEndsite());
            uEnd.setEndsite(unitInterval.getEndsite());
            uEnd.setMileage(0);

            if (unitIntervalDao.queryByStartAndEnd(unitInterval.getStartsite().getSiteId(), unitInterval.getStartsite().getSiteId()).size() == 0) {

                flag = flag && unitIntervalDao.add(uStart) > 0;
            }
            if (unitIntervalDao.queryByStartAndEnd(unitInterval.getEndsite().getSiteId(), unitInterval.getEndsite().getSiteId()).size() == 0) {
                flag = flag && unitIntervalDao.add(uEnd) > 0;
            }
        }
        if (unitIntervalDao.queryByStartAndEnd(unitInterval.getStartsite().getSiteId(), unitInterval.getEndsite().getSiteId()).size() == 0) {
            return (flag && unitIntervalDao.add(unitInterval) > 0) == true ? 1 : 0;
        }
        return 0;
    }

    @Override
    public List<UnitInterval> queryAll() {
        return unitIntervalDao.queryAll();
    }

    @Override
    public List<UnitInterval> queryByStartAndEnd(int startid,int endid) {

            return unitIntervalDao.queryByStartAndEnd(startid, endid);
    }

    @Override
    public boolean addUnitInterval(int startid, int end, int mileage, String travelTime) {
        return unitIntervalDao.addUnitInterval(startid,end,mileage,travelTime);
    }
}
